package cn.com.open.ms.ecgpaasdatarest.dynamicconfig;

import cn.com.open.ms.ecgpaasdatarest.enums.DBType;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @author 奶茶
 * Created by on 2018/11/26 10:45.
 */
@Configuration
public class DataSourceConfig {
    @Value("${spring.datasource.default.url}")
    private String defaultDBUrl;
    @Value("${spring.datasource.default.username}")
    private String defaultDBUser;
    @Value("${spring.datasource.default.password}")
    private String defaultDBPassword;
    @Value("${spring.datasource.default.driverClassName}")
    private String defaultDBDriverName;
    @Value("${spring.datasource.default.initialSize}")
    private Integer initialSize;
    @Value("${spring.datasource.default.minIdle}")
    private Integer minIdle;
    @Value("${spring.datasource.default.maxActive}")
    private Integer maxActive;
    @Value("${spring.datasource.default.maxWait}")
    private Long maxWait;
    @Value("${spring.datasource.default.filters}")
    private String filters;
    @Value("${spring.datasource.default.maxPoolPreparedStatementPerConnectionSize}")
    private Integer maxPoolPreparedStatementPerConnectionSize;

    @Bean
    public DynamicDataBaseSource dynamicDataSource() throws SQLException {
        DynamicDataBaseSource dynamicDataSource = DynamicDataBaseSource.getInstance();

        DruidDataSource defaultDataSource = new DruidDataSource();
        defaultDataSource.setUrl(defaultDBUrl);
        defaultDataSource.setUsername(defaultDBUser);
        defaultDataSource.setPassword(defaultDBPassword);
        defaultDataSource.setDriverClassName(defaultDBDriverName);
        defaultDataSource.setDbType(DBType.MYSQL.getDisplay());
        defaultDataSource.setInitialSize(initialSize);
        defaultDataSource.setMaxActive(maxActive);
        defaultDataSource.setMinIdle(minIdle);
        defaultDataSource.setValidationQuery("SELECT 1");
        defaultDataSource.setTestWhileIdle(true);
        defaultDataSource.setTestOnBorrow(false);
        defaultDataSource.setTestOnReturn(false);
        defaultDataSource.setPoolPreparedStatements(true);
        defaultDataSource.setFilters(filters);
        defaultDataSource.setMaxWait(maxWait);
        defaultDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

        Map<Object,Object> map = new HashMap<>();
        map.put("default", defaultDataSource);
        dynamicDataSource.setTargetDataSources(map);
        dynamicDataSource.setDefaultTargetDataSource(defaultDataSource);

        return dynamicDataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(
            @Qualifier("dynamicDataSource") DataSource dynamicDataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dynamicDataSource);
        return bean.getObject();

    }

    @Bean
    public DataSourceTransactionManager testTransactionManager(@Qualifier("dynamicDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(
            @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
